package com.hava.contentsale.dao;

import com.hava.contentsale.meta.ProductVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by zhanpeng on 17/11/2016.
 * 需求分析，实际为三种用户，guest，buyer，seller
 * 需求1：guest，仅仅显示内容，不显示产品状态
 * 需求2：buyer，由于只有一个用户，trx表格信息显示的为买家的购买记录，
 *
 * 注意：通过对三个表的连接查询，减少查询次数，防止出现幻读等内容。交给数据库进行处理
 *
 * 需求异常：trx没有卖家导致如果有多个卖家则需求出现逻辑错误，且无法修正。只能够假定认为只有一个卖家
 */
public interface ProductVODao {

    //R1.1.1 展示的细节为内容的标题、图片和价格。用户为Guest
    @Select("SELECT id,title,icon AS image,price FROM content")
    public List<ProductVO> findAllByGuest();

    @Select("SELECT content.id,content.title,content.icon AS image,content.price," +
                "IF((trx.personId = #{param1} AND person.userType = 0),True,False) AS isBuy " +
            "FROM content " +
                "LEFT JOIN trx ON content.id = trx.contentId " +
                "LEFT JOIN person ON trx.personId = person.id;")
    public List<ProductVO> findAllByBuyer(long user_id);

    @Select("SELECT content.id,content.title,content.icon AS image,content.price," +
                "IF((person.userType = 0),True,False) AS isSell " +
            "FROM content " +
                "LEFT JOIN trx ON content.id = trx.contentId " +
                "LEFT JOIN person ON trx.personId = person.id;")
    public List<ProductVO> findAllBySeller(long user_id);

    @Select("SELECT content.id,content.title,content.abstract AS summary,content.text AS detail,content.icon AS image,content.price,trx.price AS buyPrice " +
            "FROM content " +
                "LEFT JOIN trx ON content.id=trx.contentId " +
            "WHERE (content.id = #{param1});")
    public ProductVO findOneByGuest(long product_id);

    @Select("SELECT content.id,content.title,content.abstract AS summary,content.text AS detail,content.icon AS image,content.price,trx.price AS buyPrice," +
                "IF((trx.personId = #{param1} AND person.userType = 0),True,False) AS isBuy " +
            "FROM content " +
                "LEFT JOIN trx ON content.id=trx.contentId " +
                "LEFT JOIN person ON trx.personId=person.id " +
            "WHERE (content.id = #{param2});")
    public ProductVO findOneByBuyer(long user_id,long product_id);

    @Select("SELECT content.id,content.title,content.abstract AS summary,content.text AS detail,content.icon AS image,content.price,trx.price AS buyPrice," +
                "IF((person.userType = 0),True,False) AS isSell " +
            "FROM content " +
                "LEFT JOIN trx ON content.id=trx.contentId " +
                "LEFT JOIN person ON trx.personId=person.id " +
            "WHERE (content.id = #{param2});")
    public ProductVO findOneBySeller(long user_id,long product_id);

    ////添加Options当Mybatis插入后，源插入对象会被注入id
    @Options(useGeneratedKeys=true,keyProperty = "id")
    @Insert("INSERT INTO content (price,title,icon,abstract,text) VALUES (#{price},#{title},#{image},#{summary},#{detail})")
    public void create(ProductVO productVO);

    @Update("UPDATE content SET price=#{price},title=#{title},icon=#{image},abstract=#{summary},text=#{detail} WHERE (id = #{id})")
    public void update(ProductVO productVO);

    @Select("SELECT id,title,abstract AS summary,text AS detail,icon AS image,price " +
            "FROM content WHERE(id = #{param1})")
    public ProductVO findOne(long product_id);

    @Delete("DELETE FROM content WHERE(id = #{param1})")
    public void delete(Long id);
}
